package com.swang.pms.service.manage.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.swang.pms.dal.ManageUserDao;
import com.swang.pms.model.common.GridUtil;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.ManageUserDto;
import com.swang.pms.model.entity.ManageUser;
import com.swang.pms.service.manage.ManageUserService;

/**
 * @ClassName: ManageUserServiceImpl
 * @Description: 用户服务实现类
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Service("manageUserService")
public class ManageUserServiceImpl implements ManageUserService {

    @Resource
    private ManageUserDao manageUserDao;

    public void updateManagerUserByKeySelective(ManageUser managerUser) {
        this.manageUserDao.updateByPrimaryKeySelective(managerUser);
    }

    public ManageUserDto selectManagerUserByKey(Long managerUserId) {
        ManageUserDto dto = manageUserDao.selectByPrimaryKey(managerUserId);
        if (dto == null) {
            dto = new ManageUserDto();
        }
        dto.setId(dto.getManagerUserId());
        return dto;
    }

    public void saveAdmin(ManageUserDto dto) throws Exception {
        if (dto.getId() == null) {
            String userName = dto.getUserName();
            int count = this.manageUserDao.countUserCode(userName);
            if (count > 0) {
                ManageUserDto dto1 = this.manageUserDao.selectsBySelf(userName);
                if (dto1 != null && !dto1.getStatus().equals("delete")) {
                    throw new Exception("账号已存在");
                }
                dto.setId(dto1.getId());
                dto.setStatus("enabled");
                this.manageUserDao.updateByPrimaryKeySelective(dto);
                return;
            }
            dto.setGmtCreate(new Date());
            dto.setGmtModify(new Date());
            this.manageUserDao.insertManagerUser(dto);
            Long keyId = dto.getId();
            if (keyId == null) {
                throw new Exception("新增时无主键");
            }
        } else {
            this.manageUserDao.updateByPrimaryKeySelective(dto);
        }
        if (dto.getRoleId() != null && StringUtils.isNotBlank(dto.getUserType())) {
            this.manageUserDao.deleteRoleOfManager(dto.getId());
            this.manageUserDao.insertRoleOfManager(dto.getId(), dto.getRoleId());
        }
    }

    public Pager findPageDataByDto(Pager page, ManageUserDto dto) {
        String[] paramSerial = new String[] { "id", "user_name", "source", "password", "salt", "email", "telephone", "real_name", "remark",
                "status", "user_type", "role_name", "last_login_time", "login_num", "gmt_create", "gmt_modify", "operator" };
        dto.setColumns(Arrays.asList(paramSerial).toString().replaceAll("[\\[\\]\\s]", ""));
        dto.setStartIndex(page.getRowStartIndex());
        dto.setPageSize(page.getPageSize());
        List<Map<String, Object>> list = this.manageUserDao.findPageDataByDto(dto);

        dto.setStartIndex(null);
        dto.setPageSize(null);
        page.setRowCount(this.manageUserDao.findPageDataCountByDto(dto));
        page.setGridr(GridUtil.getGridList(list, paramSerial, page.getRowStartIndex(), page.getPageSize(), page.getRowCount(), true));
        return page;
    }

    public List<ManageUser> listYunAdmin(ManageUser admin) {
        return this.manageUserDao.listYunAdmin(admin);
    }

}
